const app = getApp()
Page({
  data: {
    imgUrls: [
      '/images/discount-banner.jpg',
      '/images/draw-banner.jpg',
      '/images/nursing-banner.jpg',
      '/images/spa-banner.jpg'
    ],
    indicatorDots: true,
    autoplay: true,

    interval: 5000,
    duration: 1000,
    goodsList: [],
    pageNo: 1,
    pageSize: 10,
    userInfo: {},
    searchLoading: false,
    searchLoadingComplete: false,
    isNoResult: false,
    isNoResultTips: "商品已售空，一大波商品正在赶来~"
  },
  onLoad: function (options) {
    let self = this;
    let userId = wx.getStorageSync("userId")
    if (userId == "") {
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }
    // self.getUserInfo();
    self.getGoodsList();
  },
  //跳转详情页面
  commodityDetail: function (e) {
    let self = this
    console.log("hahhhahahghhahgsg")
    let index = e.currentTarget.dataset.index
    console.log(self.data.goodsList[index].GoodsNo)
    console.log(e)
    wx.navigateTo({
      url: `/pages/detail/detail?goodsNo=${self.data.goodsList[index].GoodsNo}`
    })
  },
  //获取用户授权信息
  getUserInfo: function () {
    let self = this
    wx.getUserInfo({
      lang: "zh_CN",
      success: res => {
        app.globalData.userInfo = res.userInfo
        self.addMemberInfo(res.userInfo)
        self.setData({
          userInfo: res.userInfo
        })
      }
    })
  },
  //获取商品列表
  getGoodsList: function () {
    let self = this;
    wx.request({
      url: `${app.globalData.host}/api/miniapps/wxGoodsList`,
      data: {
        pageNo: self.data.pageNo,
        pageSize: self.data.pageSize
      },
      success: (result) => {
        let data = result.data;
        if (data.success) {
          if (data.data.length > 0) {
            self.data.goodsList = self.data.goodsList.concat(data.data)
            self.setData({
              goodsList: self.data.goodsList,
              searchLoading: true
            })
          } else {
            self.setData({
              searchLoadingComplete: true,
              searchLoading: false,
              isNoResult: true
            })
          }
        }
      },
      fail: (error) => {
        self.setData({
          isNoResult: true,
          isNoResultTips: error.errMsg
        })
      }
    })
  },
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上拉上拉呀")
    let self = this;
    if (self.data.searchLoading && !self.data.searchLoadingComplete) {
      self.data.pageNo++;//每次触发上拉事件，把pageCount+1 
      // self.setData({
      //   isnulldata: false
      // });
      self.getGoodsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})